package com.gupao.javavip.gof.singleton;

import java.io.Serializable;

/**
 * 饿汉式单例模式
 * <p>
 * 优点：简单直接，线程安全
 * 缺点：类初始化时变实例化，提前占用了存储空间，可能造成浪费
 * <p>
 * 这里未进行防反序列化破坏单例处理
 */
public class HungrySingletonSerializeUnSafe implements Serializable {
    //static保证类初始化时便实例化
    private static final HungrySingletonSerializeUnSafe INSTANCE = new HungrySingletonSerializeUnSafe();

    //构造方法私有化
    private HungrySingletonSerializeUnSafe() {
        if (INSTANCE != null) {
            throw new RuntimeException("不允许创建多个实例");
        }
    }

    //提供全局访问点
    public static HungrySingletonSerializeUnSafe getInstance() {
        return INSTANCE;
    }
}
